<?php
include 'util.php';

$apikey = "0d5cbb6f-218d-4f91-b32c-1f51607bb4bd";
$secret = "my-secret";
$callbackPrefix = "http://".$_SERVER['SERVER_ADDR']."/examples/GettingStarted/php/";

if (isset($_REQUEST['callee'])) {
    // devzone call $_REQUEST['callee']
    $params = "callee=".$_REQUEST['callee'];
    $result = invokeApi ("Call", $apikey, $secret, $params);    
    $callId = $result['content'];

    $status = "<b>Status:</b><br/>";
    if ($result['httpcode'] == "200") {
        $status .= "Success: Call with Call id == ".$callId."<br/>";
    } else {
        $status .= "Failure: Call<br/>";
    }

    // devzone plays announcement
    $params = "callId=".$callId."&announcement=file:///opt/voicetag-record.wav";
    $result = invokeApi ("Play", $apikey, $secret, $params);

    if ($result['httpcode'] == "200") {
        $status .= "Success: Announcement<br/>";
    } else {
        $status .= "Failure: Announcement<br/>";
    }

    // devzone starts recorder
    $params = "callId=".$callId;
    $result = invokeApi ("Record", $apikey, $secret, $params);

    if ($result['httpcode'] == "200") {
        $status .= "Success: Start recorder<br/>";
    } else {
        $status .= "Failure: Start recorder<br/>";
    }

    // devzone starts tone detector   
    $params = "callId=".$callId."&callbackUrl=".$callbackPrefix."/CallAndRecordCallback.php";
    $result = invokeApi ("StartToneDetector", $apikey, $secret, $params);

    if ($result['httpcode'] == "200") {
        $status .= "Success: Start tone detector<br/>";
    } else {
        $status .= "Failure: Start tone detector<br/>";
    }
?>

<html><body>
    <?=$status?>
</body></html>

<?php
} else {
?>

<html><body><form>

    Phone # to call:<br/>
    <input type="text" name="callee" value=""/>
    <br/>

    <input type="submit" value="Call and Record"/>

</form></body></html>

<?php
}
?>
